package com.mimc.controller.admin;

import cn.hutool.core.util.IdUtil;
import com.jfinal.aop.Inject;
import com.jfinal.core.paragetter.Para;
import com.jfinal.plugin.activerecord.Page;
import com.mimc.core.model.SysMenu;
import com.mimc.core.model.SysRole;
import com.mimc.core.model.SysRoleMenu;
import com.mimc.core.service.SysMenuService;
import com.mimc.core.service.SysRoleMenuService;
import com.mimc.core.service.SysRoleService;
import com.mimc.framework.domain.AjaxResult;
import com.mimc.service.ShSysMenuService;
import io.jboot.db.model.Columns;
import io.jboot.web.controller.JbootController;
import io.jboot.web.controller.annotation.RequestMapping;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.List;

@RequestMapping("/admin/sys/role")
@Slf4j
public class SysRoleController extends JbootController {


    @Inject
    private SysRoleService sysRoleService;

    @Inject
    private SysRoleMenuService sysRoleMenuService;

    @Inject
    private SysMenuService sysMenuService;

    @Inject
    private ShSysMenuService shSysMenuService;
    public AjaxResult lists(){
        Integer page=getInt("page",1);
        Integer pageSize=getInt("pageSize",15);
        Page<SysRole> list=sysRoleService.paginate(page,pageSize);
        return AjaxResult.success("成功",list);
    }
    public AjaxResult add(@Para("")SysRole sysRole){
        sysRole.setCreateTime(new Date());
        sysRole.setUpdateTime(new Date());
        Integer roleId= (Integer) sysRoleService.save(sysRole);
        String menuidsStr=getPara("menuId");
        String[] menuIds=menuidsStr.split(",");
        for (int i=0;i<menuIds.length;i++){
            String menuIdstr=menuIds[i];
            SysMenu sysMenu=sysMenuService.findById(Integer.parseInt(menuIdstr));
            if(sysMenu!=null){
                SysRoleMenu sysRoleMenu=new SysRoleMenu();
                long id = IdUtil.getSnowflakeNextId();
                sysRoleMenu.setId(id);
                sysRoleMenu.setMenuId(sysMenu.getMenuId());
                sysRoleMenu.setRoleId(roleId);
                sysRoleMenuService.save(sysRoleMenu);
            }
        }
        return AjaxResult.success("成功");
    }
    public AjaxResult detail(Integer roleId){
        List<SysMenu> menuList=sysMenuService.findListByColumns(Columns.create(),"sort asc");
        List<SysMenu> menuTree=shSysMenuService.buildMenuTree(menuList);
        AjaxResult ajaxResult=AjaxResult.success();
        SysRole role=sysRoleService.findById(roleId);
        List<SysRoleMenu> roleList=sysRoleMenuService.findListByColumns(Columns.create("role_id",roleId));
        ajaxResult.put("menuTree",menuTree);
        ajaxResult.put("role",role);
        ajaxResult.put("roleList",roleList);
        return ajaxResult;
    }

    public AjaxResult update(@Para("")SysRole sysRole){
        sysRole.setUpdateTime(new Date());
        if(sysRoleService.update(sysRole)){
            String menuidsStr=getPara("menuId");
            String[] menuIds=menuidsStr.split(",");
            List<SysRoleMenu> sysRoleMenus=sysRoleMenuService.findListByColumns(Columns.create("role_id",sysRole.getId()));
            for (int i=0;i<sysRoleMenus.size();i++){
                sysRoleMenuService.delete(sysRoleMenus.get(i));
            }
            for (int i=0;i<menuIds.length;i++){
                String menuIdstr=menuIds[i];
                SysMenu sysMenu=sysMenuService.findById(Integer.parseInt(menuIdstr));
                if(sysMenu!=null){
                    SysRoleMenu sysRoleMenu=new SysRoleMenu();
                    long id = IdUtil.getSnowflakeNextId();
                    sysRoleMenu.setId(id);
                    sysRoleMenu.setMenuId(sysMenu.getMenuId());
                    sysRoleMenu.setRoleId(sysRole.getId());
                    sysRoleMenu.setCreateTime(new Date());
                    sysRoleMenuService.save(sysRoleMenu);
                }
            }
        }

        return AjaxResult.success("成功");
    }
    public AjaxResult delete(Integer roleId){
        SysRole role=sysRoleService.findById(roleId);
        List<SysRoleMenu> sysRoleMenus=sysRoleMenuService.findListByColumns(Columns.create("role_id",role.getId()));
        for (int i=0;i<sysRoleMenus.size();i++){
            sysRoleMenuService.delete(sysRoleMenus.get(i));
        }
        sysRoleService.delete(role);
        return AjaxResult.success("成功");
    }

    public AjaxResult alllists(){
        List<SysRole> list=sysRoleService.findListByColumns(Columns.create(),"sort asc");
        return AjaxResult.success("成功",list);
    }
}
